﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Jewelnoid
{
    class ParticleManager
    {
        public List<Particle> particlesList;
        public Texture2D particleTexture;
        public int particlesNumber;
        public int particlesSpeed;

        public ParticleManager(Texture2D particleTexture)
        {
            this.particlesList = new List<Particle>();
            this.particleTexture = particleTexture;

            this.particlesNumber = 8;
            this.particlesSpeed = 3;
        }

        public void draw(SpriteBatch spriteBatch)
        {
            foreach (Particle particle in particlesList)
            {
                particle.draw(spriteBatch);
            }
        }

        public void update()
        {
            List<Particle> removeList = new List<Particle>();

            foreach (Particle particle in this.particlesList)
            {
                if (particle.alpha == 0f)
                {
                    removeList.Add(particle);
                }

                particle.update();
            }

            foreach (Particle particleToRemove in removeList)
            {
                this.particlesList.Remove(particleToRemove);
            }
        }

        /// <summary>
        ///     Adiciona as partículas a lista para dar o efeito. 
        ///     Position é a posição a partir de onde ocorrerá o efeito e Direction é um vetor de -1, 0 ou 1 que indica para onde será o efeito (esquerda, direta, cima ou para baixo)
        /// </summary>
        /// <param name="position"></param>
        /// <param name="direction"></param>
        public void createParticlesEffect(Vector2 position, Vector2 direction)
        {
            Color color;
            Vector2 speed;
            Random random;
            int randomNumber;
            int randomMax = 10;

            for (int i = 0; i < this.particlesNumber; i++)
            {
                random = new Random((i+1) * (int)(position.X + position.Y));
                randomNumber = random.Next(0, randomMax);

                if (direction.X == 0)
                {
                    if (i < this.particlesNumber / 2)
                    {
                        speed = new Vector2(randomNumber, (randomMax - randomNumber) * direction.Y);
                    }
                    else
                    {
                        speed = new Vector2(randomNumber * (-1), (randomMax - randomNumber) * direction.Y);
                    }
                }
                else
                {
                    if (i < this.particlesNumber / 2)
                    {
                        speed = new Vector2((randomMax - randomNumber) * direction.X, randomNumber);
                    }
                    else
                    {
                        speed = new Vector2((randomMax - randomNumber) * direction.X, randomNumber * (-1));
                    }
                }

                speed.Normalize();
                speed *= this.particlesSpeed;

                if (i < this.particlesNumber / 3)
                {
                    color = Color.Red;
                }
                else if (i < 2 * this.particlesNumber / 3)
                {
                    color = Color.Yellow;
                }
                else
                {
                    color = Color.Orange;
                }

                Particle particle = new Particle(this.particleTexture, position, color, speed);

                this.particlesList.Add(particle);
            }
        }
    }
}
